import 'package:flutter/material.dart';
import 'package:flutter_general/pages/appBar/appBarPage.dart';
import 'package:flutter_general/pages/bannerView/BannerViewPage.dart';
import 'package:flutter_general/pages/dashedLine/DashedLinePage.dart';
import 'package:flutter_general/pages/map/mapPage.dart';
import 'package:flutter_general/pages/rating/rating.dart';
import 'package:flutter_general/pages/sandClock/SandLockPage.dart';
import 'package:flutter_general/pages/scanner/scannerPage.dart';
import 'package:flutter_general/pages/scrollviewSliverTitlebar/scrollview_sliver_titlebar.dart';
import 'package:flutter_general/pages/stateController/stateControllerPage.dart';
import 'package:flutter_general/pages/takePic/takePicPage.dart';
import 'package:pkg_obz/export.dart';
import 'package:pkg_obz/tools/date_formatter.dart';
import 'package:pkg_obz/widgets/dialog.dart';
import 'package:pkg_obz/widgets/item_widgets.dart';
import 'package:pkg_obz/widgets/refresh_list_view.dart';
import 'package:plugin_get_version/plugin_get_version.dart';

import 'indexPageModel.dart';

/// Create： Zhang 2021/6/28 10:03 上午
/// erp:     ext.zhangjunfei
/// 描述：索引页

class IndexPage extends PageStateful<IndexPageModel> {
  IndexPage() : super(title: "使用示例", pageModel: IndexPageModel());

  @override
  Widget? leading(BuildContext context) {
    return Container();
  }

  @override
  Widget? content(BuildContext context) {
    return SafeArea(
      child: refreshListView(
        controller: pageModel.refreshController,
        refresh: pageModel.refreshData,
        onLoadingMore: pageModel.loadMoreData,
        dividerColor: Colors.transparent,
        itemCount: pageModel.indexItems.length,
        isNeedLoadingMore: true,
        itemBuilder: (context, index) {
          return itemWidget(
            context,
            text: "${index + 1}.${pageModel.indexItems[index]}",
            onClick: () {
              switch (pageModel.indexItems[index]) {
                case "弹框":
                  showDialog(context);
                  break;
                case "日期格式化":
                  showAlert(
                    msg: DateFormatter.formatDateTime(DateTime.now()),
                    actions: [AlertDialogAction()],
                  );
                  break;
                case "时间选择":
                  pickTime(context);
                  break;
                case "照片选择":
                  ZRouter.goTo(TakePicPage());
                  break;
                case "扫描二维码":
                  ZRouter.goTo(ScannerPage());
                  break;
                case "获取版本号":
                  PluginGetVersion.appVersionName
                      .then((value) => toast("ver:$value"));
                  break;
                case "百度地图":
                  ZRouter.goTo(MapPage());
                  break;
                case "通用标题栏":
                  ZRouter.goTo(AppBarPage());
                  break;
                case "沙漏加载":
                  ZRouter.goTo(SandLockPage());
                  break;
                case "轮播图":
                  ZRouter.goTo(BannerViewPage());
                  break;
                case "虚线":
                  ZRouter.goTo(DashedlinePage());
                  break;
                case "评星":
                  ZRouter.goTo(Rating());
                  break;
                case "SliverAppbar":
                  ZRouter.goTo(ScrollViewSliverAppbar());
                  break;
                case "状态管理":
                  ZRouter.goTo(StateControllerPage());
                  break;
              }
            },
          );
        },
      ),
    );
  }

//-------------方法定义----------------

  ///展示弹框
  showDialog(BuildContext context) {
    showBottomMenu(
        items: ["提示", "失败", "成功", "toast", "loading", "对话框"],
        callback: (index, item) {
          switch (item) {
            case "提示":
              toastInfo("提示消息");
              break;
            case "失败":
              toastError("错误消息");
              break;
            case "成功":
              toastSuc("成功消息");
              break;
            case "toast":
              toast("提示消息");
              break;
            case "loading":
              toastLoading();
              Future.delayed(Duration(seconds: 2))
                  .then((value) => dismissLoading());
              break;
            case "对话框":
              showAlert(
                msg: "提示消息",
                actions: [AlertDialogAction()],
              );
              break;
          }
        });
  }

  ///时间选择
  void pickTime(BuildContext context) {
    showDateTimePicker(onConfirm: (time) {
      toast(time.millisecondsSinceEpoch.toString());
    });
  }
//-------------方法定义----------------
}
